package com.th.therp.service.Impl;

import com.th.therp.exception.BaseException;
import com.th.therp.pojo.dto.UpdateUserDto;
import com.th.therp.pojo.emtity.User;
import com.th.therp.pojo.dto.UserDto;
import com.th.therp.mapper.UserMapper;
import com.th.therp.pojo.vo.LoginVo;
import com.th.therp.pojo.vo.UserVo;
import com.th.therp.service.UserService;
import com.th.therp.utils.JwtUtils;
import com.th.therp.utils.RegValidUtils;
import com.th.therp.utils.SmsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SmsUtils smsUtils;

    @Autowired
    private RegValidUtils regValidUtils;

    @Override
    public void register(UserDto userDto) {
        //用户注册要检验的地方太多，弄个注册校验工具类来校验
        regValidUtils.isValid(userDto);

        User user = new User();
        String newPassword = DigestUtils.md5DigestAsHex(userDto.getPassword().getBytes());

        user.setPhone(userDto.getPhone());
        user.setUsername(userDto.getUsername());
        user.setPassword(newPassword);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.register(user);
    }

    @Override
    public LoginVo login(UserDto userDto) {
        String newPassword = DigestUtils.md5DigestAsHex(userDto.getPassword().getBytes());
        userDto.setPassword(newPassword);

        User user = userMapper.login(userDto);

        if(user != null){
            Map<String,Object> claims  = new HashMap<>();
            claims.put("id",user.getId());
            claims.put("username",user.getUsername());
            claims.put("role",user.getRole());
            LoginVo loginVo = new LoginVo();
            loginVo.setToken(JwtUtils.generateJwt(claims));
            loginVo.setRole(user.getRole());
            return loginVo;
        }else {
            throw new BaseException("登录失败");
        }
    }

    @Override
    public String findNameById(Long userId) {
        return userMapper.findNameById(userId);
    }

    @Override
    public void sendCode() throws Exception {
        // 判断手机号是否已存在
        String phone = userMapper.findAdminPhone();
        smsUtils.sendSms(phone);
    }

    @Override
    public void update(UpdateUserDto updateUserDto) {
        //查询当前名称是否存在
        UserDto userDto = userMapper.findByUsername(updateUserDto.getUsername());
        String oldPassword = userMapper.findPassWordById(updateUserDto.getId());

        //如果当前名称存在 并且 查到的名称的id与传进来的名称的id不相同
        if (userDto != null && !updateUserDto.getId().equals(userDto.getId())) {
            throw new BaseException("该用户名称已存在");
        }
        if (updateUserDto.getPassword() == null) {
            updateUserDto.setPassword(oldPassword);
        }else {
            String newPassword = DigestUtils.md5DigestAsHex(updateUserDto.getPassword().getBytes());
            updateUserDto.setPassword(newPassword);
        }
        userMapper.update(updateUserDto);
    }

    @Override
    public List<UserVo> list() {
        return userMapper.list();
    }

    @Override
    public void delete(List<Integer> ids) {
        ids.forEach(id->{
            userMapper.delete(id);
        });
    }
}
